package ui;

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Image;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class ImageViewer {

	private JFrame frame;
	private JLabel lblNewLabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					ImageViewer window = new ImageViewer();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ImageViewer() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		//frame.setBounds(100, 100, 800, 600);
		frame.setExtendedState(Frame.MAXIMIZED_BOTH);  
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		JScrollPane scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		lblNewLabel = new JLabel("");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		scrollPane.setViewportView(lblNewLabel);
	}

	public static void showImageViewer(String title, String path) {
		ImageViewer imageViewer = new ImageViewer();
		imageViewer.initialize();
		imageViewer.frame.setTitle(title);
		File file = new File(path);
		if (!file.exists())
			imageViewer.lblNewLabel.setText("图片不存在");
		else {
			ImageIcon icon = new ImageIcon(path);
			imageViewer.lblNewLabel.setIcon(new ImageIcon(icon.getImage().getScaledInstance(-1, 600, Image.SCALE_SMOOTH)));
		}
		imageViewer.frame.setVisible(true);
	}
}
